package com.bicon.botu.finance.wallet.handler;

import com.alibaba.fastjson.JSONObject;
import com.bicon.botu.finance.wallet.common.WalletConstants;
import com.bicon.botu.core.base.common.utils.StringUtils;
import com.bicon.botu.core.base.repository.redis.RedisHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

/**
 * @Author: 宋旭
 * @Date: Created in 10:47 2017/7/15/015
 */
@Component
public class WalletHandler {

    @Autowired
    RedisHandler redisHandler;

    /**
     * 缓存用户余额
     * @param userId
     * @param balance
     */
    public void cacheBalance(String userId, BigDecimal balance){
        String balanceStr = redisHandler.getGlobalRedisTemplate().getStringValue(WalletConstants.BOTU_WALLET_BALANCE,userId);
        if (null != balanceStr){
            redisHandler.getGlobalRedisTemplate().remove(WalletConstants.BOTU_WALLET_BALANCE,userId);
        }
        redisHandler.getGlobalRedisTemplate().cacheStringValue(WalletConstants.BOTU_WALLET_BALANCE,userId,String.valueOf(balance));
    }

    /**
     * 获取余额
     * @param userId
     * @return
     */
    public String getBalance(String userId){
        return redisHandler.getGlobalRedisTemplate().getStringValue(WalletConstants.BOTU_WALLET_BALANCE,userId);
    }

    /**
     * 获取用户名称
     * @param userId
     * @return
     */
    public String getUserName(String userId){
        String userInfo = redisHandler.getGlobalRedisTemplate().getStringValue(WalletConstants.BOTU_USER_USER_INFO,userId);
        if(StringUtils.isBlank(userInfo)){
            return null;
        }
        JSONObject jsonObject = JSONObject.parseObject(userInfo);
        String name = jsonObject.getString("name");
        return name;
    }
}
